﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using com.tiyma.components;

using System.ServiceProcess;
using System.Text;

namespace com.tiyma.service
{
    public partial class SEOService : ServiceBase
    {
        System.Threading.Thread m_thd = null;

        System.Threading.Thread m_crawler_thd = null;

        System.Threading.Thread m_crawler_content_thd = null;

        public SEOService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            BookIndexManager t_book_mgr = new BookIndexManager();

            CrawlerManager t_crawler_mgr = new CrawlerManager();
            try
            {
                m_crawler_thd = new System.Threading.Thread(t_crawler_mgr.WatchCrawler);

                if (m_crawler_thd != null)
                {
                    m_crawler_thd.Start();

                    LogHelper.Info("成功创建监视crawler的线程!");
                }

                m_crawler_content_thd = new System.Threading.Thread(t_crawler_mgr.WatchCrawlerChapterContent);

                if (m_crawler_content_thd != null)
                {
                    m_crawler_content_thd.Start();

                    LogHelper.Info("成功创建监视章节内容收录的线程!");
                }

                m_thd = new System.Threading.Thread(t_book_mgr.CreateBookIndexByService);

                if (m_thd != null)
                {
                    m_thd.Start();

                    LogHelper.Info("成功创建创建索引的线程!");
                }

              

            }
            catch (Exception ex)
            {
                LogHelper.Error("服务启动失败" + ex.ToString());
            }

        }

        protected override void OnStop()
        {
            try
            {
                if (m_thd != null)
                {
                    m_thd.Abort();
                    LogHelper.Info("线程已经停止!");
                }

                if (m_crawler_thd != null)
                {
                    m_crawler_thd.Abort();
                    LogHelper.Info("监控线程已经停止!");
                }

                if (m_crawler_content_thd != null)
                {
                    m_crawler_content_thd.Abort();
                    LogHelper.Info("监控章节内容线程已经停止!");
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("关闭线程失败" + ex.ToString());
            }
        }
    }
}
